
function getLoginCode(provider) {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: provider,
			success: function(res) {
				console.log("code res: ", res)
				if (res && res.code) {
					resolve(res.code)
				} else {
					reject("获取 code 失败")
				}
			},
			fail: function(res) {
				console.log("code fail: ", res)
				reject("获取 code 失败")
			}
		});
	})
}

function getProvider() {
	return new Promise((resolve, reject) => {
		uni.getProvider({
			service: 'oauth',
			success: function(res) {
				console.log("provider res: ", res.provider)
				if (res && res.provider) {
					resolve(res.provider)
				} else {
					reject("获取 provider 失败")
				}
			},
			fail: function() {
				reject("获取 provider 失败")
			}
		});
	})
}


function loginCode() {
	return new Promise(async(resolve, reject) => {
		let res = await getProvider()
		if (res && res.length > 0) {
			let provider = res[0]
			let code = await getLoginCode(provider)
			resolve({provider,code});
		}else{
			reject('非小程序平台')
		}
	})
}

export default {
	loginCode
}